<template>
  <div id="app">
    <el-container v-if="!isMobileRoute" class="app-container">
      <Sidebar 
        :is-collapse="isCollapse"
        :warning-count="warningCount"
      />
      <el-container class="main-container" :class="{ 'is-collapsed': isCollapse }">
        <el-header class="header">
          <div class="header-left">
            <i 
              :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'" 
              class="toggle-sidebar" 
              @click="toggleSidebar"
            ></i>
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item v-for="(item, index) in breadcrumbs" :key="index">
                {{ item }}
              </el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          <div class="header-right">
            <el-dropdown trigger="click">
              <span class="user-info">
                <img src="https://cdn-icons-png.flaticon.com/512/1077/1077114.png" class="user-avatar">
                管理员
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>个人信息</el-dropdown-item>
                <el-dropdown-item>修改密码</el-dropdown-item>
                <el-dropdown-item divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>
        <el-main class="main">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
    <router-view v-else />
  </div>
</template>

<script>
import Sidebar from '@/layout/components/Sidebar.vue'

export default {
  name: 'App',
  components: {
    Sidebar
  },
  data() {
    return {
      isCollapse: false,
      breadcrumbs: [],
      warningCount: 12 // 模拟预警数量
    }
  },
  computed: {
    isMobileRoute() {
      return this.$route.path.startsWith('/mobile')
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.getBreadcrumbs(route)
      },
      immediate: true
    }
  },
  methods: {
    toggleSidebar() {
      this.isCollapse = !this.isCollapse
    },
    getBreadcrumbs(route) {
      // 简单的面包屑生成逻辑
      const pathArray = route.path.split('/').filter(i => i)
      this.breadcrumbs = pathArray.map(path => {
        switch(path) {
          case 'dashboard':
            return '控制台'
          case 'amazon-products':
            return '亚马逊商品'
          case 'inventory':
            return '库存管理'
          case 'sku':
            return 'SKU库存'
          case 'records':
            return '出入库记录'
          case 'warning':
            return '库存预警'
          case 'warning-rules':
            return '预警规则'
          case 'purchase':
            return '采购管理'
          default:
            return path
        }
      })
    }
  }
}
</script>

<style lang="scss">
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background-color: #f5f7fa;
}

#app {
  height: 100vh;
  color: var(--text-primary);
}

.app-container {
  height: 100vh;
  overflow: hidden;
}

.main-container {
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  margin-left: 220px;
  transition: margin-left 0.3s;
  
  &.is-collapsed {
    margin-left: 64px;
  }
}

.header {
  background-color: #fff;
  color: #333;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  padding: 0 20px;
  position: relative;
  z-index: 1;
}

.header-left {
  display: flex;
  align-items: center;
}

.toggle-sidebar {
  font-size: 20px;
  margin-right: 15px;
  cursor: pointer;
}

.user-info {
  cursor: pointer;
  display: flex;
  align-items: center;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 8px;
  border: 2px solid var(--primary-color);
  background-color: #fff;
  padding: 2px;
}

.main {
  background-color: #f5f7fa;
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}
</style> 